﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using KerriganBlog;

namespace KerriganClient.View
{
    public partial class ListSessions : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            try
            {
                using (ServiceProxy srv = new ServiceProxy(Request.Cookies))
                {
                    this.ListSessionsGrid.DataSource = srv.Service.GetSessionsAvailableByCustomerID(this.GetCustomerId(Request.Cookies));
                    this.ListSessionsGrid.DataBind();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        protected void ListSessionsGrid_Paging(object sender, GridViewPageEventArgs e)
        {

            this.ListSessionsGrid.PageIndex = e.NewPageIndex;
            RefreshDataGrid();
        }


        protected void ListSessionsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
        }


        protected void ListSessionsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //if (e.CommandName == "MyDelete")
            //{
            //    using (ServiceProxy Srv = new ServiceProxy())
            //    {
            //        int id = Convert.ToInt32(e.CommandArgument);
            //        Srv.Service.DeleteCategory(id);
            //        RefreshDataGrid();
            //    }
            //}
        }

        protected void RefreshDataGrid()
        {
            this.ListSessionsGrid.DataSource = null;
            using (ServiceProxy Srv = new ServiceProxy(Request.Cookies))
            {
                this.ListSessionsGrid.DataSource = Srv.Service.GetSessionsAvailableByCustomerID(this.GetCustomerId(Request.Cookies));
                this.ListSessionsGrid.DataBind();
            }

        }

        protected void lnkRegister_OnClick(object sender, EventArgs e)
        {

            if ((sender as Button).CommandArgument != null)
            {
                using (ServiceProxy Srv = new ServiceProxy(Request.Cookies))
                {
                    if (Srv.Service.AddSessionToUser(int.Parse((sender as Button).CommandArgument.ToString()), (this.GetCustomerId(Request.Cookies))))
                    {
                        Response.Redirect("~/View/ListSessions.aspx");
                    }
                }
            }
        }
    }
}